package ve.edu.ucla.dals.servicio.server;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;

import ve.edu.ucla.dals.modelo.ConexionSQL;
import ve.edu.ucla.dals.servicio.ManejadorDespacho;

public class Loader {
	private static Handler prepareMainHandler(DispatchConfigurator dispatcherConfigurator) {
		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        
        ServletDispatcher dispatcher = new ServletDispatcher(context);
        dispatcherConfigurator.configure(dispatcher);

        HandlerCollection mainHandler = new HandlerCollection();
		mainHandler.setHandlers(new Handler[] { new LogHandler(), context });
		
		return mainHandler;
	}

	public static Server start(DispatchConfigurator dispatcherConfigurator, int port) {
		Server server = new Server(port);
        try {
        	Handler handler = prepareMainHandler(dispatcherConfigurator);
        	server.setHandler(handler);
        	
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return server;
	}
}
